<?php
include("general_include.php");
include "checklogin.php";
include "top.php";
$user_ladder_id = $_REQUEST['user_ladder_id'];
$extra_content = "";

if(isset($_POST['submit']))
{
   
	/*echo '<pre>';
	print_r($_REQUEST);
	  exit;*/
   $Formval = $_POST;
   $lesson_id  = $_POST['lesson_id'];
   $student_id = $_POST['student_id'];
   $coach_id = $_SESSION['user_id'];
   $lesson_price = $_POST['lesson_price'];
   $lesson_title = stripslashes($_POST['lesson_title']);
   $lesson_description = stripslashes($_POST['lesson_description']);
   
   $AdminComisionSql="SELECT default_comision_value FROM ".TABLEPREFIX."_default_comision";
   $AdminComision = $UserManagerObjAjax->GetRecords("Row",$AdminComisionSql);
   
   $lesson_commision_admin = $AdminComision['default_comision_value'];
   
   $lesson_status_id = 1;
   $valid=1;
  
   
	   if(empty($lesson_id))
	   {
			   $Sql_ins= "INSERT INTO ".TABLEPREFIX."_lesson 
						  SET lesson_title = '".$lesson_title."',
							  lesson_description = '".$lesson_description."',
							  coach_id = '".$coach_id."',
							  student_id = '".$student_id."',
							  lesson_price = '".$lesson_price."',
							  lesson_commision_admin = '".$lesson_commision_admin."',
							  lesson_status_id = '".$lesson_status_id."',
							  date_edited='".date("Y-m-d H:i:s")."',
							  date_added='".date("Y-m-d H:i:s")."'";
			  $UserManagerObjAjax->Execute($Sql_ins);
			  
  			   if(!mysql_error())
			   {
				 // header("location:my_game.php");
				 	$SelectEmailStudent="SELECT name,email FROM ".TABLEPREFIX."_user WHERE user_id=".$student_id;
					$EmailStudent=$UserManagerObjAjax->GetRecords("Row",$SelectEmailStudent);
				 
				    $to       = $EmailStudent['email'];
					$subject  = 'Lesson Assigned';
					$message  = '<h3>Dear '.$EmailStudent['name'].',</h3>';
					$message .= 'You have been assigned to the lesson '.$lesson_title;
					$headers  = 'MIME-Version: 1.0' . "\r\n";
					$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
					$headers .= 'From: info@citytechsoftware.com' . "\r\n" .
								'Reply-To: sales@citytechsoftware.com' . "\r\n" .
								'X-Mailer: PHP/' . phpversion();
					
					mail($to, $subject, $message, $headers);
					
				    echo "<script>window.location.href='add_lesson.php'</script>";
			   }
				   
	   }
	   else if(!empty($lesson_id))
	   {
			   $Sql= "UPDATE ".TABLEPREFIX."_lesson 
					   SET lesson_title = '".$lesson_title."',
						   lesson_description = '".$lesson_description."',
						   student_id = '".$student_id."',
						   lesson_price = '".$lesson_price."'
					   WHERE lesson_id = '".$lesson_id."'";  
			   $UserManagerObjAjax->Execute($Sql);

			   if(!mysql_error())
			   {
				  echo "<script>window.location.href='my_lesson.php'</script>";
			   }
	   }	   

}

if(!empty($_REQUEST['lesson_id']))
{
	/* Get Record For Display Starts */	

	$SelectgameSql="SELECT * FROM ".TABLEPREFIX."_lesson WHERE lesson_id='".$_REQUEST['lesson_id']."'";
	$Formval = $UserManagerObjAjax->GetRecords("Row",$SelectgameSql);
    $SubmitButton="Edit";
	
	$Formval['lesson_title']		= htmlspecialchars(addslashes($Formval['lesson_title']));
	$Formval['lesson_price']		= htmlspecialchars(addslashes($Formval['lesson_price']));
	$Formval['lesson_description']	= htmlspecialchars(addslashes($Formval['lesson_description']));
	$Formval['student_id']			= $Formval['student_id'];
	$Formval['lesson_id']			= $Formval['lesson_id'];
	
	$proSql = "SELECT * FROM ".TABLEPREFIX."_user WHERE  user_id='".$Formval['student_id']."'";
	$proArr= $UserManagerObjAjax->GetRecords("Row",$proSql);
	$Formval['student'] = $proArr['name'];
}
else
{
	$SubmitButton="Add";
	$Formval['lesson_id'] = '';
}

$StudentSql = "select user.user_id,user.name FROM ".TABLEPREFIX."_user as user,".TABLEPREFIX."_user_type as type,".TABLEPREFIX."_user_type_user_relation as rel where rel.user_id=user.user_id and rel.user_type_id=type.user_type_id and type.user_type_id=2 and user.is_active='Y'";
$StudentArr = $UserManagerObjAjax->HtmlOptionArrayCreate($StudentSql);

$smarty->assign('is_coach',$is_coach);
$smarty->assign('is_partner',$is_partner);
$smarty->assign('extra_content',$extra_content);
$smarty->assign('Formval',$Formval);
$smarty->assign('ermsg',$ermsg);
$smarty->assign('Formval',$Formval);
$smarty->assign('SubmitButton',$SubmitButton);
$smarty->assign('StudentArr',$StudentArr);
$smarty->assign('user_game_id',$user_game_id);
$smarty->display("lesson_update.tpl");
include("footer.php");
?>